package cfengMiddleware.server.rabbitmq.consumer;

import cfengMiddleware.server.rabbitmq.entity.MessageEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author Cfeng
 * @date 2022/9/23
 */

@Component
@Slf4j
@EnableScheduling
@RequiredArgsConstructor
public class RedissonDelayQueueConsumer {

    private final RedissonClient redissonClient;

    @Scheduled(cron = "*/1 * * * *")
    public void consumeMsg() throws Exception {
        //定义延迟队列
        final String key = "redissonDelayQueueV3";
        RBlockingQueue<MessageEntity> rBlockingQueue = redissonClient.getBlockingQueue(key);
        //从队列中取出消息
        MessageEntity messageEntity = rBlockingQueue.take();
        if(!Objects.isNull(messageEntity)) {
            log.info("Redisson延迟消息 消费者： 监听消息： {}",messageEntity);
        }
    }
}
